﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_Fin_Fin003 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["ZYstatus"] != null)
            {
                drpTransferStatus.SelectedValue = Session["ZYstatus"].ToString();
            }
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["ZYstatus"] = drpTransferStatus.SelectedValue;
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinShippingQuery q1 = new FinShippingQuery("q1");
        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFThirdUnitQuery q6 = new BFThirdUnitQuery("q6");

        ArrayList args = new ArrayList();
        args.Add(q1.CreatedDttm >= txtStartDate.Value);
        args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        if (!string.IsNullOrEmpty(drpTransferStatus.SelectedValue))
        {
            args.Add(q1.ShippingStatus >= drpTransferStatus.SelectedValue);
        }
        q1.InnerJoin(q2).On(q1.ShippingStatus==q2.Code);
        q1.LeftJoin(q3).On(q1.TransferType == q3.Code);
        q1.LeftJoin(q4).On(q1.TransportType == q4.Code);
        q1.LeftJoin(q5).On(q1.FromDestination == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.ToDestination == q6.ThirdUnitID);
        q1.Select(q1.ShipID, q1.CreatedBy, q1.CreatedDttm, q5.Name.As("FromDestination"), q6.Name.As("ToDestination"),q6.ThirdType,
            q1.IsChecked,q1.Check1,q1.Check2,q1.Sender,q1.ShippingDate,q1.ReceivedBy,q1.ReceivedDttm,q1.Comments,q1.AttachmentName,  q2.CodeDesc.As("ShippingStatus"), q3.CodeDesc.As("TransferType"), q3.CodeDesc.As("TransportType"))
            .Where(args.ToArray());
        q1.OrderBy(q1.CreatedDttm.Descending);
        
        
        //VShippingInfoCollection shipBoxs = new VShippingInfoCollection();

        //ArrayList args = new ArrayList();
        //args.Add(shipBoxs.Query.CreatedDttm >= txtStartDate.Value);
        //args.Add(shipBoxs.Query.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        //if (!string.IsNullOrEmpty(drpTransferStatus.SelectedValue))
        //{
        //    args.Add(shipBoxs.Query.ShippingStatus >= drpTransferStatus.SelectedValue);
        //}
        //if (!LoginUserInfo.CurrentUser.IsAdmin)
        //{
        //    args.Add(shipBoxs.Query.FromDestination == LoginUserInfo.CurrentUser.ThirdUnitID);
        //    //shipBoxs.Query.Where(shipBoxs.Query.FromDestination == LoginUserInfo.CurrentUser.ThirdUnitID);
        //}

        //shipBoxs.Query.Where(args.ToArray());
        //shipBoxs.Query.OrderBy(shipBoxs.Query.ShippingStatus.Ascending, shipBoxs.Query.CreatedDttm.Descending);

        pageGrid.DataSource = q1.LoadDataTable();
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("ZYstatus");
    }

    protected string GetActionUrl(string status, object rowView)
    {
        string retUrl = "";
        switch (status)
        {
            case ConstantValue.ShippingStatusCode.New:
                retUrl = GetLink("添加凭证盒", "A", "Fin003b.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetLink("提交确认", "A", "Fin003e.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetUpdateLink("Fin003a.aspx", rowView, 680, 580, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetDeleteLink("Fin003a.aspx", rowView, VShippingInfoMetadata.ColumnNames.ShipID);
                break;
            case ConstantValue.ShippingStatusCode.NeedReply://等待回复
                retUrl = GetLink("添加凭证盒", "A", "Fin003b.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
                break;
        }



        //if (status.Equals(ConstantValue.ShippingStatusCode.Pending))
        //{
            
        //    if (boxCount > 0)
        //    {
        //        retUrl += "   " + GetLink("确认转移", "A", "Fin003d.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
        //    }

            
        //}

        return retUrl;
    }



    protected string getBoxCount(object Id,object status)
    {
        string result = "0";
        FinShippingBoxesCollection finShippingBoxesCollection = new FinShippingBoxesCollection();
        finShippingBoxesCollection.Query.Where(finShippingBoxesCollection.Query.ShipID==int.Parse(Id.ToString()));
        finShippingBoxesCollection.Query.Load();

        if (status.ToString() == ConstantValue.ShippingStatusCode.New)
        {
            result = finShippingBoxesCollection.Count.ToString() + "[<a class='style4' href=\"javascript:OpenWindow('凭证盒管理','V','/Admin/Fin/Fin003c.aspx?type=V&ShipID=" + Id.ToString() + "',980,680)\">凭证盒管理</a>]";
        }
        else
        {
            result = finShippingBoxesCollection.Count.ToString();
        }


        return result;
    }

    protected string getThirdName(object name, object type)
    {
        if (type.ToString() == "JGLX003")
            return name.ToString();
        else
            return "否";
    }
}